package info.wegosoft.android.activity;

import java.util.ArrayList;
import java.util.List;


import info.wegosoft.R;
import info.wegosoft.android.adapter.PlatformAdapter;
import info.wegosoft.android.model.Platform;
import info.wegosoft.android.qauth.sina.OAuthConstant;
import info.wegosoft.android.qauth.sina.WeiboException;
import info.wegosoft.android.qauth.sina.http.AccessToken;
import info.wegosoft.android.qauth.sina.http.RequestToken;
import info.wegosoft.android.util.CacheUtil;
import info.wegosoft.android.util.RecentActivityUtil;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;

/**  
 * Filename:    WeiboPlatformListActivity.java 
 * Package:     info.wegosoft.android.activity 
 * Description: TODO(这里用一句话描述这个类的作用)
 * Copyright:   Copyright (c) wegosoft.info 2011  
 * @author:     李辉 
 * email:       fzlihui@gmail.com
 * @version:    1.0  
 * Create at:   2011-5-18 下午10:38:15  
 */
public class MBlogPlatformListActivity extends ListActivity {

	private PlatformAdapter platformAdapter;
	
	private static List<Platform> platformList;
	
	//初始化时载入数据的
	private ProgressDialog loadCacheProgressDialog;
	
	static{
		
		platformList = new ArrayList<Platform>();
		
		Platform sinaPlatform = new Platform(Platform.SINA_PLATFORM, R.drawable.sina_logo);
		
		Platform tencentPlatform = new Platform(Platform.TENCENT_PLATFORM, R.drawable.tencent_logo);
		
		Platform renrenPlatform = new Platform(Platform.RENREN_PLATFORM,R.drawable.renrenlogo);
		
		platformList.add(sinaPlatform);
		
		platformList.add(tencentPlatform);
		
		platformList.add(renrenPlatform);
		
		
	}
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.platform_list);
		//记录当前activity
		RecentActivityUtil.recentActivity = this;
		
		Uri uri = this.getIntent().getData();
		
		//如果是获取了新浪的AccessToken后重新跳转到这个activity
		if(uri!=null && uri.getQueryParameter("oauth_verifier")!=null){
			
			try {
				RequestToken requestToken = OAuthConstant.getInstance().getRequestToken();
				
				AccessToken accessToken = requestToken.getAccessToken(uri.getQueryParameter("oauth_verifier"));
				
				SharedPreferences preference = getSharedPreferences("sina_qauth",Context.MODE_PRIVATE);
				
				Editor editor = preference.edit(); 
				
				editor.putString("AccessToken", accessToken.getToken());
				
				editor.putString("AccessTokenSecret", accessToken.getTokenSecret());
				
				//这里accessToken.getScreenName()返回空
				int userId = accessToken.getUserId();
				
				String userName = OAuthConstant.getInstance().getWeibo().showUser(Integer.toString(userId)).getName();
				
				editor.putString("userName", userName);
				
				editor.commit();
				
			} catch (WeiboException e) {
				e.printStackTrace();
			}
		}
		
		platformAdapter = new PlatformAdapter(this, R.layout.platform_item, platformList);
		
		this.setListAdapter(platformAdapter);
	}
	
	public void loadCache(){
		
		//显示Progress对话框
		loadCacheProgressDialog = ProgressDialog.show(MBlogPlatformListActivity.this,null,"载入中",true);
		
		new Thread()
	    { 
	        public void run()
	        { 
	        	SharedPreferences preference = MBlogPlatformListActivity.this.getSharedPreferences("platform",Context.MODE_PRIVATE);
	        	
	        	int platType = preference.getInt("type", -1);
	        	
	        	try {
	        			
//					CacheUtil.readDateFromLocalXmlCache(WeiboPlatformListActivity.this,platType);
	        		CacheUtil.readDateFromLocalJsonCache(MBlogPlatformListActivity.this,platType);
					
				} catch (Exception e) {
					System.out.println("本地缓存文件不存在");
				}
				Intent intent = new Intent();
				intent.setClass(MBlogPlatformListActivity.this, HomeTabsActivity.class);
				startActivity(intent);
	            loadCacheProgressDialog.dismiss();    
	        }
	    }.start(); //开始运行运行线程 			
	    
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {

			new AlertDialog.Builder(MBlogPlatformListActivity.this)
					.setTitle("真的要离开？")
					.setMessage("确定要离开?")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
																
									MBlogPlatformListActivity.this.finish();
								
								}
							}).setNegativeButton("取消", null).show();

		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onDestroy() {
		  
		  //配合WeiboPlatformListActivity.this.finish();
//		  System.exit(0);

		  //或者下面这种方式
		  android.os.Process.killProcess(android.os.Process.myPid());
	}
	
	
}
